<template>
  <div class="person">
    <h1>情况三：监视【reactive】定义的【对象类型】数据</h1>
    <h2>name: {{ person.name }}</h2>
    <h2>age: {{ person.age }}</h2>
    <button @click="changeName">changeName</button>
    <button @click="changeAge">changeAge</button>
    <button @click="changePerson">changePerson</button>
  </div>
</template>

<script lang="ts" setup name="Person">
import { watch, reactive } from "vue";
// 1. data
let person = reactive({
  name: "Jack",
  age: 18,
});
// 2. function
function changeName() {
  person.name += "~";
}
function changeAge() {
  person.age += 1;
}
function changePerson() {
  // reactive 包裹的对象，需要用 Object.assign() 方法修改
  Object.assign(person, { name: "Tom", age: 21 });
}
// 3. watch
// 情况三：监视【reactive】定义的【对象类型】数据，默认就是开启 deep 监视的（即修改属性值，会触发监视）
watch(person, (newVal, oldVal) => {
  console.log("person change", newVal, oldVal);
});
</script>

<style scoped>
.person {
  background-color: skyblue;
  box-shadow: 0 0 10px;
  border-radius: 10px;
  padding: 20px;
}
button {
  margin: 0 5px;
}
</style>
